export default function buildNavigationTree(rows) {
    const result = {};
    const positionGroups = {};
    rows.forEach(item => {
      if (!positionGroups[item.position]) {
        positionGroups[item.position] = [];
      }
      positionGroups[item.position].push(item);
    });
    for (const position in positionGroups) {
      const items = positionGroups[position];
      const itemMap = {};
      const tree = [];
      items.forEach(item => {
        itemMap[item.id] = {
          ...item,
          children: []
        };
      });
      items.forEach(item => {
        if (item.parent_id) {
          if (itemMap[item.parent_id]) {
            itemMap[item.parent_id].children.push(itemMap[item.id]);
          }
        } else {
          tree.push(itemMap[item.id]);
        }
      });
      tree.sort((a, b) => a.sort - b.sort);
      tree.forEach(item => {
        if (item.children.length > 0) {
          item.children.sort((a, b) => a.sort - b.sort);
        }
      });
      result[position] = tree;
    }
    return result;
  }